<!-- Horizontal Form -->
<div class="box box-info">
	<div class="box-header with-border">
		<!--原user_edit页面 -->
		<h3 class="box-title">修改类目信息</h3>
		<h5 class="msg"></h5>
	</div>
	<!-- /.box-header -->
	<!-- form start -->
	<form class="form-horizontal">
		<div class="box-body">
			<div class="form-group">
				<label for="usernameId" class="col-sm-2 control-label">分类名称名</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" id="usernameId"
						name="username">
				</div>
			</div>
			<div class="form-group" id="pwdDiv">
				<label for="passwordId" class="col-sm-2 control-label">分类描述</label>

				<div class="col-sm-10">
					<input type="text" class="form-control" id="passwordId">
				</div>
			</div>
			<div class="form-group">
				<label for="emailId" class="col-sm-2 control-label">排序</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" id="emailId">
				</div>
			</div>
			<div class="form-group">
				<label for="emailId" class="col-sm-2 control-label">创建时间</label>
				<div class="col-sm-10">
					<p id="creatTime"></p>
				</div>
			</div>
			<!-- /.box-body -->
			<div class="box-footer">
				<button type="button" class="btn btn-default btn-cancel">取消</button>
				<button type="button" class="btn btn-info pull-right btn-save">确定添加</button>
				<!-- <a href="../starter.html" target="_blank">测试超链接</a> -->
			</div>
			<!-- /.box-footer -->
	</form>

</div>
<script type="text/javascript"
	src="bower_components/ztree/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="bower_components/layer/layer.js"></script>
<script type="text/javascript">
	$(document).ready(
			function() {

				doInitFormData();

				console.log("ready-Date数据", $("#mainContentId").data("data").id);
				//页面加载完成以后加载角色信息并初始化页面
				//doLoadRoles();
				//事件注册
				$(".box-footer").on("click", ".btn-cancel", doCancel).on(
						"click", ".btn-save", doSaveOrUpdate);
				//$("form").on("blur", "#usernameId", isExists);
			});
	/* 判断类名是否存在 */
	/* function isExists() {
		var filedName = $(this).prop("name");
		var filedValue = $(this).val();
		var params = {
			"filedName" : filedName,
			"filedValue" : filedValue
		};
		var url = "user/isExists";
		$.getJSON(url, params, function(result) {
			if (result.data) {
				$(".msg").html(filedName + "已存在");
			} else {
				$(".msg").html(" ");
			}
		});
	} */

	function doCancel() {
		//$("#mainContentId").removeData("rowData");
		$("#mainContentId").load("category_list");
	}
	function doSaveOrUpdate() {
		console.log("doSaveOrUpdate-id数据", $("#mainContentId").data("data").id);
		/* var rowData = $("#mainContentId").data("rowData"); */
		//1.获取表单数据
		var params = doGetEditFormData();
		console.log("新获得的params", params);
		/* if (rowData)
			params.id = rowData.user.id; */
		//2.发起异步请求
		var insertUrl = "category/updateCategoryObject";
		//var updateUrl = "user/doUpdateObject";
		//var url = rowData ? updateUrl : insertUrl;
		console.log("params页面数据", params);
		$.post(insertUrl, params, function(result) {
			if (result.status == 1) {
				alert(result.message);
				doCancel();
			} else {
				alert(result.message);
			}
		})
	}
	/*============================================================= */
	function doGetEditFormData() {
		console.log("id数据", $("#mainContentId").data("id"));
		//获取用户输入的数据
		var params = {
			id :  $("#mainContentId").data("data").id,
			categoryName : $("#usernameId").val(),
			categoryDescription : $("#passwordId").val(),
			sort : $("#emailId").val(),
		}
		return params;
	}
	function doInitFormData() {
		console.log("doInitFormData-id",$("#mainContentId").data("data").id);
		var data = $("#mainContentId").data("data");
		if (!data)
			return;
		//初始化用户信息 
		$("#usernameId").val(data.categoryName);
		$("#passwordId").val(data.categoryDescription);
		$("#emailId").val(data.sort);
		$("#creatTime").html(new Date(data.createdTime).toLocaleString());
	}

	/* 	function doInitPageRoles(data) {
	 //1.获取角色要显示的位置对象
	 var div = $("#rolesId");
	 div.empty();
	 //2.迭代数据，将数据追加到div
	 var input = "<input type='checkbox' name='roleId' value='[id]'>[name]"
	 for ( var i in data) {
	 //记住每次replace时都会产生一个新的字符串对象
	 var newInput = input.replace("[id]", data[i].id).replace("[name]",
	 data[i].name);
	 div.append(newInput)
	 }
	 } */
</script>






